package cn.lyjuan.first.hadoop.demo.ch06;

import cn.lyjuan.base.util.DateUtils;
import cn.lyjuan.first.hadoop.demo.enums.ChNameEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.service.launcher.HadoopUncaughtExceptionHandler;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * HBase基本的增删改查操作
 */
@Slf4j
public class Ch06S01Api {
    private static final TableName tn = TableName.valueOf(ChNameEnum.CH06.name());

    private static final byte[] f1 = Bytes.toBytes("f1");
    private static final byte[] f2 = Bytes.toBytes("f2");

    private static final byte[] f1_name = Bytes.toBytes("name");
    private static final byte[] f1_age = Bytes.toBytes("age");
    private static final byte[] f2_tail = Bytes.toBytes("tail");
    private static final byte[] f2_weight = Bytes.toBytes("weight");

    public static void main(String[] args) throws IOException {
//        create();

//        put();

//        del();

        get();
    }

    /**
     * 获取
     */
    public static void get() throws IOException {
        Table t = ConnUtil.conn().getTable(tn);


        Get get = new Get(Bytes.toBytes(1));
        get.addFamily(f1);
        Result r = t.get(get);

        List<Cell> cs = r.listCells();
        Map<String, String> result = new HashMap<>();
        for (Cell c : cs) {
            result.put(CellUtil.getCellKeyAsString(c), Bytes.toString(CellUtil.cloneValue(c)));
        }

        System.out.println("Get result ==> " + result);
    }

    /**
     * 删除
     *
     * @throws IOException
     */
    public static void del() throws IOException {
        Table t = ConnUtil.conn().getTable(tn);
        LocalDateTime time = DateUtils.parseTime("2021-07-24 12:45:35.500", "yyyy-MM-dd HH:mm:ss.SSS");
        // 第1行
        Delete del = new Delete(Bytes.toBytes(1));
        del.addColumn(f1, f1_age);
        t.delete(del);
        t.close();

        ConnUtil.close();
        log.info("Del SUCC");
    }

    /**
     * 插入/更新数据<br/>
     * 更新也是用PUT，使用新的timestamp加入新值
     */
    public static void put() throws IOException {
        Table t = ConnUtil.conn().getTable(tn);

        Put put = new Put(Bytes.toBytes(1));
        put.addColumn(f1, f1_name, Bytes.toBytes("zhangsan"));
        put.addColumn(f1, f1_age, Bytes.toBytes(18));
        put.addColumn(f2, f2_tail, Bytes.toBytes(175));
        put.addColumn(f2, f2_weight, Bytes.toBytes(65));
        t.put(put);
        log.info(tn.getNameAsString() + " Put zhangsan SUCC");

        put.addColumn(f1, f1_name, Bytes.toBytes("lisi"));
        put.addColumn(f1, f1_age, Bytes.toBytes(19));
        put.addColumn(f2, f2_tail, Bytes.toBytes(176));
        put.addColumn(f2, f2_weight, Bytes.toBytes(66));
        t.put(put);
        log.info(tn.getNameAsString() + " Put lisi SUCC");

        t.close();
        ConnUtil.close();


    }

    /**
     * 创建表
     */
    public static void create() throws IOException {
        Admin admin = null;
        admin = ConnUtil.conn().getAdmin();

        if (admin.tableExists(tn))
            return;

        TableDescriptor td = TableDescriptorBuilder.newBuilder(tn)
                .setColumnFamily(ColumnFamilyDescriptorBuilder.of("f1"))
                .setColumnFamily(ColumnFamilyDescriptorBuilder.of("f2"))
                .build();

        admin.createTable(td);
        admin.close();
        ConnUtil.close();
    }
}
